/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package atm.gui;

import atm.Atm;
import atm.connection.Connect;

import com.alee.extended.label.WebMultiLineLabel;
import com.alee.extended.panel.GroupPanel;
import com.alee.laf.StyleConstants;
import com.alee.laf.label.WebLabel;
import com.alee.laf.panel.WebPanel;
import com.alee.laf.scroll.WebScrollPane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.swing.ScrollPaneConstants;

/**
 *
 * @author Quark
 */
public class ScheduleATMPanel extends WebPanel {
    String header;
    
    WebLabel headerLabel;
    WebScrollPane atmsListScrollPane;
    WebMultiLineLabel atmsListLabel;
    GroupPanel atmsListPanel;
    private Connect connection;
    int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;

    public ScheduleATMPanel(String header, int daysFromToday) {
        this.setLayout(new BorderLayout());
        
        this.header = header;
        
        headerLabel = new WebLabel(header, WebLabel.CENTER);
        headerLabel.setDrawShade(true);
        headerLabel.setForeground(new Color(222, 222, 222));
        headerLabel.setShadeColor(Color.black);
        headerLabel.setBackground(new Color(240, 240, 240));
        headerLabel.setMargin(8);
        headerLabel.setOpaque(true);
        headerLabel.setPreferredWidth(142);
        
        /*TextBubbleBorder roundedLabelBorder = new TextBubbleBorder(null, 0, 7, 0);
        roundedLabelBorder.setBorderInsets(8);
        headerLabel.setBorder(roundedLabelBorder);
        */
        this.setUndecorated(false);
        //this.setRound(StyleConstants.largeRound);
        //this.setBackground(Color.white);
        
        this.add(headerLabel, BorderLayout.NORTH);
        
        atmsListScrollPane = new WebScrollPane(createListPanel(daysFromToday), false);
        atmsListScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        atmsListScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        atmsListScrollPane.setPreferredHeight(290);
        atmsListScrollPane.setBackground(null);
        this.add(atmsListScrollPane);
        //this.add(atmsListLabel, BorderLayout.SOUTH);
    }
    
    private GroupPanel createListPanel(int daysFromToday) {
        atmsListPanel = new GroupPanel(3, false);
        connection = new Connect();
        connection.getConnected();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/YYYY");
        String dateToComparison = dateFormat.format(date.getTime() + daysFromToday * MILLIS_IN_DAY);
        System.out.println("Currnent date: "  + dateToComparison);
        ArrayList<Atm> atms = connection.getNextScheduleDays();
        
        for (int i = 0; i < atms.size(); i++) {
    		String dateFromDatabase = dateFormat.format(atms.get(i).getNastepne_doladowanie());
        	if(dateToComparison.equals(dateFromDatabase)){
	        	String streetAndCityName = atms.get(i).getCity() + " " + atms.get(i).getStreet() + " " + atms.get(i).getStreetNumber();
	            atmsListPanel.add(new ScheduleMiniPanel(streetAndCityName, atms.get(i).getLiczba_10(), atms.get(i).getLiczba_20(), atms.get(i).getLiczba_50(), atms.get(i).getLiczba_100()));
        	}
        }
                
        atmsListPanel.setMargin(16);
        atmsListPanel.setUndecorated(true);
        atmsListPanel.setBackground(null);
        //atmsListPanel.setPreferredHeight(390);
        
        return atmsListPanel;
    }
    
}
